package com.dshore.messagecenter.controller.system;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.model.system.User;
import com.dshore.messagecenter.service.basedata.LoginService;
import com.dshore.messagecenter.service.exception.PasswordNotMatchException;
import com.dshore.messagecenter.service.exception.UsernameNotFoundException;
import com.dshore.messagecenter.util.IPUtil;
import com.dshore.messagecenter.util.UUIDUtil;

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;
/**
 * 登录控制类
 * @author panyi
 * @Date 2018/7/18
 * @version 1.00
 *
 */
@Controller
@RequestMapping("/user")
public class LoginController{
	
	@Autowired
	private LoginService loginService;
	
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "system/login";
	}
	
	//@LogAnnotation(methodType="登录",targetType="系统",argsName="username")
	@RequestMapping(value="/handlelogin.do",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void>handleLogin(@RequestParam("username") String username,@RequestParam("password")String password,HttpSession session,HttpServletResponse response,HttpServletRequest request){
		ResponseResult<Void> rr;
		try {
			User user=loginService.login(username, password);
			rr=new ResponseResult<Void>(1);
			User u=new User();
			u.setId(user.getId());
			u.setUsername(user.getUsername());
			u.setPassword(user.getPassword());
			
			request.getSession().setAttribute("user", username);
			
			//向登录记录中插入ip相关数据
			String br = IPUtil.getIpAddr(request);
	        String province =  IPUtil.getCity(br);
	        String agent= request.getHeader("user-agent");
	        String osName = null;
	        String uaFamily=null;
	        try {
	            UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(agent);
	            //操作系统
	            osName = userAgentInfo.getOsName();
	            //浏览器名称
	            uaFamily = userAgentInfo.getUaFamily();
	        } catch (IOException e) {
	           
	        	e.printStackTrace();
	        }
	        //插入数据库
	        Map<String,Object> map=new HashMap<>();
	        map.put("id", UUIDUtil.getUUId());
	        map.put("ip", br);
	        map.put("city", province);
	        map.put("system", osName);
	        map.put("browser",uaFamily);
	        Integer ip=loginService.addIp(map);
	        
	        
		} catch (UsernameNotFoundException e) {
			rr=new ResponseResult<Void>(0,e);
		} catch(PasswordNotMatchException e) {
			rr=new ResponseResult<Void>(-1,e);
		}
		return rr;
		
	}
	
	//method=RequestMethod.POST方法必须要写
	@RequestMapping(value="/changePassword",method=RequestMethod.POST)
	@ResponseBody
	public Integer changePassword(@RequestParam("user")String username,@RequestParam("pwd")String password) {
		Integer res=loginService.changePassword(username,password);
		return res;
		
	}
	
	//退出登录
	@RequestMapping(value="/loginout")
	@ResponseBody
	public void loginout(HttpServletRequest request, HttpServletResponse response) throws IOException{
		 request.getSession().removeAttribute("user");
	     response.sendRedirect(request.getContextPath()+"/user/toLogin");

	}

    public static class UserAgentUtil {

           static   UASparser uasParser = null;

             // 初始化uasParser对象
           static    {
                     try {
                             uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
                         } catch (IOException e) {
                             e.printStackTrace();
                     } }}
}
